<?php

namespace app\index\traits;

use app\index\service\payments\PaymentInterface;
use app\index\service\payments\ProxyPaymentInterface;
use app\index\service\payments\TransferBatchInterface;
use app\index\service\payments\TransferInterface;
use think\facade\Config;

/**
 * 支付
 * Trait PaymentTrait
 * @package app\index\traits
 */
trait PaymentTrait
{

    /**
     * 获取付款支付实现
     * @param $payMethod
     * @return PaymentInterface|null
     */
    private function getPayment($payMethod): ?PaymentInterface
    {
        // 优先从配置获取
        $className = Config::get("payment.payments.$payMethod.className", null);
        if (not_blank($className)) {
            return app($className);
        }

        return app_instance(PaymentInterface::class, $payMethod, '', 'PaymentService');
    }

    /**
     * 获取转账支付实现
     * @param $payMethod
     * @return TransferInterface|null
     */
    private function getTransfer($payMethod): ?TransferInterface
    {
        // 优先从配置获取
        $className = Config::get("payment.transfers.$payMethod.className", null);
        if (not_blank($className)) {
            return app($className);
        }

        return app_instance(TransferInterface::class, $payMethod, '', 'PaymentService');
    }

    /**
     * 获取批量转账支付实现
     * @param $payMethod
     * @return TransferBatchInterface|null
     */
    private function getTransferBatch($payMethod): ?TransferBatchInterface
    {
        // 优先从配置获取
        $className = Config::get("payment.transfersbatch.$payMethod.className", null);
        if (not_blank($className)) {
            return app($className);
        }

        return app_instance(TransferBatchInterface::class, $payMethod, '', 'PaymentService');
    }

    /**
     * 获取付款支付实现
     * @param $payMethod
     * @return ProxyPaymentInterface|null
     */
    private function getProxyPayment($payMethod): ?ProxyPaymentInterface
    {
        // 优先从配置获取
        $className = Config::get("payment.payments.$payMethod.className", null);
        if (not_blank($className)) {
            return app($className);
        }

        return app_instance(ProxyPaymentInterface::class, $payMethod, '', 'PaymentService');
    }
}